package com.example.mytest.annotation1;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName: ParamConstraintValidated
 * @Description: 验证器类需要实现ConstraintValidator泛型接口
 * 该demo用于参数验证
 * @Author: 230689
 * @Date: 2021/9/3 15:56
 * @Version: v1.0
 */
public class ParamConstraintValidated implements ConstraintValidator<Check, Object> {
    /**
     * 合法的参数值，从注解中获取
     * */
    private List<String> paramValues;


    @Override
    public void initialize(Check constraintAnnotation) {
        //初始化时获取注解上的值
        paramValues = Arrays.asList(constraintAnnotation.paramValues());

    }


    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {

        if (paramValues.contains(o)) {
            return true;
        }

        //不在指定的参数列表中
        return false;
    }
}